'use strict';

// ====== CLASS DBConn ========================================
// DB ConnectionWrapper for Access2000 DB
//
// Copyright 2005 SiC. All Rights Reserved.
// Last Update: 2005-02-26 23:57:15
// ============================================================

class DBConn {

  constructor(app) {

    this.app = app;

    // Private
    this.dbConnection = this.app.mysql;

    // Public
    this.opened = false;
    this.recordCount = 0;
    this.returnRecordCount = 0;
    this.queryCount = 0;
    // Debug
    this.debugData = [];
  }

  // Open DB Connection ----------------------------
  async open() {
    if (!this.opened) {
      this.opened = true;
    }
  }

  // Close DB Connection ----------------------------
  async close() {
    if (this.opened) {
      this.opened = false;
    }
  }

  // DB Query ----------------------------------
  // intPageSize & intCurrentPage is optional 指定后代表查询部分，否则查询所有
  async query(strSQL, intPageSize, intCurrentPage) {
    // Check If the Connection is opened
    if (!this.opened) {
      await this.open();
    }

    this.queryCount++;

    // Query
    const primarySQL = strSQL;
    if (intPageSize !== undefined) { // 查询所有
      intCurrentPage = Math.max(intCurrentPage || 0, 1);
      intPageSize = Math.max(intPageSize, 1);

      const limitStart = (intCurrentPage - 1) * intPageSize;

      strSQL = `${strSQL} LIMIT ${limitStart}, ${intPageSize}`;
    }

    console.log(strSQL);

    const tmpRS = await this.app.mysql.query(strSQL);

    // todo 联表查询时，count语法还有问题
    const countSQL = primarySQL.replace(/SELECT (\*|[\w|_|,]+) FROM/i, 'SELECT count(*) as lbs_count FROM');
    const countResult = await this.app.mysql.queryOne(countSQL);

    this.recordCount = countResult ? countResult.lbs_count : 0;

    this.returnRecordCount = tmpRS.length;

    return tmpRS;
  }

  // DB Update ----------------------------------
  async update(strTable, arrValue, strWhere) {
    const result = await this.app.mysql.update(strTable, arrValue, { where: strWhere });

    return result.affectedRows === 1;
  }

  // DB Update ----------------------------------
  async updateSQL(strTable, strValue, strWhere) {
    return await this.update(strTable, strValue, strWhere);
  }

  // DB Insert ----------------------------------
  async insert(strTable, arrValue) {
    const result = await this.app.mysql.insert(strTable, arrValue);

    return result.affectedRows === 1;
  }

  // DB Delete ----------------------------------
  async doDelete(strTable, strWhere) {
    const result = await this.app.mysql.delete(strTable, strWhere);
    return result.affectedRows === 1;
  }

  // Simple Execute ----------------------------------
  async exec(strSQL) {
    if (!strSQL) return false;

    this.queryCount++;

    // Check If the Connection is opened
    if (!this.opened) {
      await this.open();
    }

    return await this.app.mysql.query(strSQL);
  }

  literal(literal) {
    return new this.dbConnection.literals.Literal(literal);
  }

}

module.exports = DBConn;
